package com.spring.sunflower.conversation

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.spring.sunflower.R
import com.spring.sunflower.bean.ChatGiftBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.DoubleClickUtils
import java.util.HashMap

/**
 * Created by Kevin on 2021/8/23<br/>
 *
 * Describe:<br/>
 */
class BottomGiftDialog(var giftData: List<ChatGiftBean.Data>, money: String, context: Context) :
    BottomPopupView(context) {
    private var recyclerView: RecyclerView? = null
    private var mAdapter: GiftAdapter? = null
    private var data: MutableList<ChatGiftBean.Data> = ArrayList()
    private var clickPos = 0
    private var tvDonate: TextView? = null
    private var tvCharge: TextView? = null
    public var tvMoney: TextView? = null
    private var mUserID: String = ""
    var money: String = money
        set(value) {
            tvMoney?.text = money
        }

    override fun getImplLayoutId(): Int {
        return R.layout.layout_bottom_gift_dialog
    }

    override fun onCreate() {
        super.onCreate()
        recyclerView = findViewById(R.id.rvRecyclerView)
        tvDonate = findViewById(R.id.tvDonate)
        tvCharge = findViewById(R.id.tvCharge)
        tvMoney = findViewById(R.id.tvMoney)
        mUserID = SPUtils.getInstance().getString(Constant.KEY_USERID, "")
        val mOtherID = SPUtils.getInstance().getString(Constant.KEY_USERID_CHATTER, "")
        val gridLayoutManager = GridLayoutManager(context, 4)
        recyclerView?.layoutManager = gridLayoutManager
        mAdapter = GiftAdapter(R.layout.adapter_chat_gift, data)
        recyclerView?.adapter = mAdapter
        mAdapter!!.addData(giftData)
        val mon = SPUtils.getInstance().getString(Constant.KEY_MONEY, "0")
        tvMoney!!.setText(mon)
//        mPresenter.loadAllGift()
//        mPresenter.getDetail(mUserID)
        mAdapter?.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                clickPos = position
                listener?.onGiftSelect(position)
                for (index in data.indices) {
                    data[index].checked = index == position
                }
                mAdapter?.notifyDataSetChanged()

            }

        })
        tvDonate?.setOnClickListener {
            if (DoubleClickUtils.isFastLongClick()) {
                ToastUtils.showShort("请不要频繁操作")
                return@setOnClickListener
            }
            val map: MutableMap<String, String> = HashMap()
            map["MID"] = mUserID
            map["anchorId"] = mOtherID
            map["giftId"] = data[clickPos].id.toString()
            map["number"] = "1"
            map["roomId"] = "0"
            map["type"] = "1"
            listener?.onSendGift(map,clickPos)
        }
        tvCharge?.setOnClickListener {
            listener?.onChargeClick()
        }


    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context) * .5f).toInt()
    }

    interface OnGiftActionListener {
        fun onSendGift(map: MutableMap<String, String>,pos:Int)
        fun onChargeClick()
        fun onGiftSelect(position: Int)

    }

    var listener: OnGiftActionListener? = null
    open fun setOnGiftActionListener(l: OnGiftActionListener) {
        listener = l;
    }

    fun updateMoney(money: String) {
        tvMoney!!.setText(money)
        SPUtils.getInstance().put(Constant.KEY_MONEY, money)
    }
}